package string_question.leecode.test8;

import org.omg.PortableInterceptor.INACTIVE;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/6 17:47
 */
public class Solution1 {

    // 字符串转化为整数
    public int myAtoi(String str) {
        if (str == null || str.length() == 0) return 0;
        int ans = 0;
        int pop = 0;
        int sign = 1;
        boolean flag = false;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == '-' && !flag) {
                sign = -1;
                flag = true;
                continue;
            }
            if (str.charAt(i) == '+' && !flag) {
                flag = true;
                continue;
            }
            if (str.charAt(i) == ' ' && !flag) {
                continue;
            }

            if (str.charAt(i) <= '9' && str.charAt(i) >= '0') {
                flag = true;
                pop = str.charAt(i) - '0';
                if (ans * sign > Integer.MAX_VALUE / 10 || (ans * sign == Integer.MAX_VALUE / 10 && pop > 7)) {
                    return Integer.MAX_VALUE;
                }
                if (ans * sign < Integer.MIN_VALUE / 10 || (ans * sign == Integer.MIN_VALUE / 10 && pop > 8)) {
                    return Integer.MIN_VALUE;
                }
                ans = ans * 10 + pop;
            } else {
                break;
            }
        }
        return ans * sign;
    }
}
